package com.smartkey.sharedpreferences;

import java.util.ArrayList;
import java.util.List;

import android.content.Context;
import android.content.SharedPreferences;
import android.util.Log;

import com.smartkey.bean.AppInfo;

public class SharePreferencesUtil {

	private static final String TAG = "SharePreferencesUtil";

	private Context context;

	public SharePreferencesUtil(Context ctx) {
		this.context = ctx;
	}

	private SharedPreferences getClickPreferences() {
		SharedPreferences userInfo = context.getSharedPreferences("click_info",
				0);
		return userInfo;
	}

	private SharedPreferences getPackageNamePreferences() {
		SharedPreferences userInfo = context
				.getSharedPreferences("app_info", 0);
		return userInfo;
	}

	private SharedPreferences getConfigPreferences() {
		SharedPreferences config = context.getSharedPreferences("smartkey", 0);
		return config;
	}

	/**
	 * 设置闪光灯状态
	 * 
	 * @param flashLightStatus
	 */
	public void setFlashLightTag(boolean flashLightStatus) {
		getConfigPreferences().edit()
				.putBoolean("flashLightTag", flashLightStatus).commit();
	}

	/**
	 * 获取闪关灯状态
	 * 
	 * @return
	 */
	public boolean getFlashLightTag() {
		return getConfigPreferences().getBoolean("flashLightTag", false);
	}

	/**
	 * 设置单击启动的值
	 */
	public void setSingleClickValue(AppInfo appInfo) {
		SharedPreferences pre = getClickPreferences();
		SharedPreferences.Editor editor = pre.edit();
		editor.putString("SingleClick", appInfo.appId.getValue());
		if (!appInfo.isDefault) {
			editor.putString("SingleClick", appInfo.packageName);
		}
		editor.putBoolean("SingleClick_isDefault", appInfo.isDefault);
		editor.commit();
		System.out.println("################# setSingleClickValue="
				+ appInfo.appName);
	}

	/**
	 * 设置双击启动的值
	 */
	public void setDoubleClickValue(AppInfo appInfo) {
		SharedPreferences pre = getClickPreferences();
		SharedPreferences.Editor editor = pre.edit();
		editor.putString("DoubleClick", appInfo.appId.getValue());
		if (!appInfo.isDefault) {
			editor.putString("DoubleClick", appInfo.packageName);
		}
		editor.putBoolean("DoubleClick_isDefault", appInfo.isDefault);
		editor.commit();
		System.out.println("################# setDoubleClickValue="
				+ appInfo.appName);
	}

	/**
	 * 设置三连击启动值
	 * 
	 * @param appInfo
	 */
	public void setTrifecta(AppInfo appInfo) {
		SharedPreferences pre = getClickPreferences();
		SharedPreferences.Editor editor = pre.edit();
		editor.putString("Trifecta", appInfo.appId.getValue());
		if (!appInfo.isDefault) {
			editor.putString("Trifecta", appInfo.packageName);
		}
		editor.putBoolean("Trifecta_isDefault", appInfo.isDefault);
		editor.commit();
		System.out.println("################# setTrifecta=" + appInfo.appName);
	}

	/**
	 * 获取单击启动的值 默认为拍照
	 */
	public String getSingleClickValue() {
		SharedPreferences pre = getClickPreferences();
		String val = pre.getString("SingleClick",
				AppInfo.APPID.TAKE_PHOTO.getValue());
		Log.d(TAG, "单击开启的app包名为：" + val);
		return val;
	}

	/**
	 * 获取单击app是否默认应用
	 * 
	 * @return
	 */
	public boolean isDefaultSingle() {
		SharedPreferences pre = getClickPreferences();
		return pre.getBoolean("SingleClick_isDefault", true);
	}

	/**
	 * 获取双击启动的值 默认为录音
	 */
	public String getDoubleClickValue() {
		SharedPreferences pre = getClickPreferences();
		String val = pre.getString("DoubleClick",
				AppInfo.APPID.RECORD.getValue());
		return val;
	}

	/**
	 * 获取双击app是否默认应用
	 * 
	 * @return
	 */
	public boolean isDefaultDouble() {
		SharedPreferences pre = getClickPreferences();
		return pre.getBoolean("DoubleClick_isDefault", true);
	}

	/**
	 * 获取三连击启动的值 默认为手电筒
	 */
	public String getTrifectaClickValue() {
		SharedPreferences pre = getClickPreferences();
		String val = pre.getString("Trifecta",
				AppInfo.APPID.FLASHLIGHT.getValue());
		return val;
	}

	/**
	 * 获取三连击app是否默认应用
	 * 
	 * @return
	 */
	public boolean isDefaulTrifecta() {
		SharedPreferences pre = getClickPreferences();
		return pre.getBoolean("Trifecta_isDefault", true);
	}

	/**
	 * 把包名存到SharedPreferences
	 */
	public void setCheckAppPackageName(String packageName) {
		SharedPreferences pre = getPackageNamePreferences();
		SharedPreferences.Editor editor = pre.edit();
		String packageNamesPre = pre.getString("PackageNames", "");
		if (null == packageNamesPre || "".equals(packageNamesPre)) {
			packageNamesPre = packageName + ",";
		} else {
			packageNamesPre += packageName + ",";
		}
		editor.putString("PackageNames", packageNamesPre);
		editor.commit();
	}

	/**
	 * 获取全部用户添加的APP包名
	 */
	public List<String> getAllCheckAppPackageName() {
		SharedPreferences pre = getPackageNamePreferences();
		String packageNamesPre = pre.getString("PackageNames", "");
		ArrayList<String> packageNameArray = new ArrayList<String>();
		if (null != packageNamesPre || !"".equals(packageNamesPre)) {
			String[] packageNames = packageNamesPre.split(",");
			if (packageNames != null) {
				for (String packageName : packageNames) {
					if (null != packageName && !"".equals(packageName)) {
						packageNameArray.add(packageName);
					}
				}
			}
		}
		return packageNameArray;
	}

	/**
	 * 设置电话信息
	 */
	public void setCallNumber(String num) {
		SharedPreferences pre = getClickPreferences();
		SharedPreferences.Editor editor = pre.edit();
		editor.putString("CallNumber", num);
		editor.commit();
		System.out.println("################# setCallNumber=" + num);
	}

	/**
	 * 获取电话信息
	 */
	public String getCallNumber() {
		SharedPreferences pre = getClickPreferences();
		String val = pre.getString("CallNumber", "");
		return val;
	}
	/**
	 * 设置短信信息
	 */
	public void setSMS(String num, String msg) {
		SharedPreferences pre = getClickPreferences();
		SharedPreferences.Editor editor = pre.edit();
		editor.putString("SMSNumber", num);
		editor.putString("msg", msg);
		editor.commit();
		System.out.println("################# SMSNumber=" + num+",msg = "+msg);
	}
	
	/**
	 * 获取短信接收人
	 */
	public String getSMSNumber() {
		SharedPreferences pre = getClickPreferences();
		String val = pre.getString("SMSNumber", "");
		return val;
	}
	/**
	 * 获取短信内容
	 */
	public String getSMSMsg() {
		SharedPreferences pre = getClickPreferences();
		String val = pre.getString("msg", "");
		return val;
	}
}
